package com.yxinsur.imc.web.controller.business;

import com.yxinsur.imc.business.excel.ContractExpenseRateTemplate;
import com.yxinsur.imc.business.form.ContractExpenseDataForm;
import com.yxinsur.imc.business.form.ContractExpenseSearchForm;
import com.yxinsur.imc.business.service.IImcContractExpenseRateService;
import com.yxinsur.imc.business.vo.ContractExpenseRateVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 协议-产品费率Controller
 *
 * @author imc
 * @date 2021-08-20
 */
@Api(value = "协议产品费率配置服务", tags = {"协议产品费率配置服务"})
@RestController
@RequestMapping("/business/contract/expense/rate")
public class ImcContractExpenseRateController extends BaseController {
    @Autowired
    private IImcContractExpenseRateService imcContractFeeRateService;

    /**
     * 查询协议-产品费率列表
     */
    @ApiOperation("协议产品费率查询列表")
    @Log(title = "协议-产品费率", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(ContractExpenseSearchForm contractExpenseSearchForm) {
        startPage();
        List<ContractExpenseRateVo> list = imcContractFeeRateService.selectContractExpenseRateVoList(contractExpenseSearchForm);
        return getDataTable(list);
    }

    /**
     * 导出协议-产品费率列表
     */
    @ApiOperation("协议费率导出")
    @Log(title = "协议-产品费率", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ContractExpenseSearchForm contractExpenseSearchForm) {
        List<ContractExpenseRateVo> list = imcContractFeeRateService.selectContractExpenseRateVoList(contractExpenseSearchForm);
        ExcelUtil<ContractExpenseRateVo> util = new ExcelUtil<>(ContractExpenseRateVo.class);
        return util.exportExcel(list, "协议产品费率表");
    }

    @ApiOperation("协议费率模板下载")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<ContractExpenseRateTemplate> util = new ExcelUtil<ContractExpenseRateTemplate>(ContractExpenseRateTemplate.class);
        return util.importTemplateExcel("协议产品费率模板表");
    }

    @ApiOperation("协议费率数据导入（contractExpenseId:协议科目ID）")
    @PostMapping("/import/rate/{contractExpenseId}")
    public AjaxResult importData(MultipartFile file, @PathVariable("contractExpenseId") Long contractExpenseId) throws Exception {
        ExcelUtil<ContractExpenseRateTemplate> util = new ExcelUtil<>(ContractExpenseRateTemplate.class);
        List<ContractExpenseRateTemplate> rateList = util.importExcel(file.getInputStream());
        String message = this.imcContractFeeRateService.importExpenseRate(contractExpenseId, rateList);
        return AjaxResult.success(message);
    }


    /**
     * 获取协议-产品费率详细信息
     */
    @ApiOperation("根据id获得协议产品费率信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imcContractFeeRateService.selectImcContractExpenseRateById(id));
    }

    /**
     * 新增协议-产品费率
     */
    @ApiOperation("新增协议产品费率信息")
    @Log(title = "新增协议-产品费率", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ContractExpenseDataForm contractExpenseDataForm) {
        return toAjax(imcContractFeeRateService.insertImcContractExpenseRate(contractExpenseDataForm));
    }

    /**
     * 修改协议-产品费率
     */
    @ApiOperation("修改协议产品费率信息")
    @Log(title = "修改协议-产品费率", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ContractExpenseDataForm contractExpenseDataForm) {
        return toAjax(imcContractFeeRateService.updateImcContractExpenseRate(contractExpenseDataForm));
    }

    /**
     * 删除协议-产品费率
     */
    @ApiOperation("删除协议产品费率信息")
    @Log(title = "删除协议-产品费率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imcContractFeeRateService.deleteImcContractExpenseRateByIds(ids));
    }
}
